<template>
  <div>
    <el-table
      :data="ClientsPagedata.data.filter(
        data => !search ||data.username.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%"
    >
      <el-table-column label="头像" align="center">
        <template slot-scope="scope">
          <img
            width="50px"
            v-if="scope.row.avatarImg"
            :src="'http://localhost:3000/images/'+scope.row.avatarImg"
            alt
          />
          <img width="50px" v-else src="http://localhost:3000/images/default.jpg" alt />
        </template>
      </el-table-column>
      <el-table-column label="编号" prop="_id" width="190px" align="center"></el-table-column>
      <el-table-column label="姓名" prop="username" align="center"></el-table-column>
      <el-table-column label="年龄" prop="age" width="90px" align="center"></el-table-column>
      <el-table-column label="性别" prop="gender" align="center"></el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="$router.push('/Updatecustomers/'+scope.row._id)">修改</el-button>
          <el-button size="mini" type="danger" @click="dele(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ClientsPagedata.currentPage-0"
      :page-sizes="[3, 5, 8, 10]"
      :page-size="ClientsPagedata.pageSize-0"
      layout="total, sizes, prev, pager, next, jumper"
      :total="ClientsPagedata.total"
    ></el-pagination>
  </div>
</template>
<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
  "cosutomers"
);
export default {
  data() {
    return {
      search: "",
      // page: {
      //   totalpage: 3,
      //   currentPage: 1,
      // },
    };
  },
  computed: {
    ...mapState(["ClientsPagedata"]),
  },
  created() {
    this.getClients();
  },
  methods: {
    ...mapActions(["getClients", "deletClient"]),
    ...mapMutations(["setCurrent", "setPageSize"]),
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.setPageSize(val);
      this.getClients();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.setCurrent(val);
      this.getClients();
    },
    dele(row) {
      // 删除提示框
      this.$confirm("此操作将永久删除该客户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "danger",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.deletClient(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style>
</style>